package com.mycc.game.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mycc.game.common.Code;
import com.mycc.game.common.Result;
import com.mycc.game.damin.Information;
import com.mycc.game.damin.Scenario;
import com.mycc.game.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class InformationController {
    @Autowired
    private InformationService informationService;


    //查询所有怪谈信息
    @GetMapping("/selAllInformation/{pageNumber}/{pageSize}")
    public Result selInformationPage(@PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Information> page = informationService.selAllInformation(pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //根据id查询怪谈信息
    @GetMapping("/selInformationById/{id}/{pageNumber}/{pageSize}")
    public Result selInformationById(@PathVariable Integer id, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Information> page = informationService.selAllInformationById(id, pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //通过名字模糊查询怪谈信息
    @GetMapping("/selInformationByName/{name}/{pageNumber}/{pageSize}")
    public Result selInformationByName(@PathVariable String name, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Information> page = informationService.selAllInformationByName(name, pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //删除怪谈信息
    @GetMapping("/delInformationById/{id}")
    public Result delInformationById(@PathVariable Integer id) {
        boolean flag = informationService.delInformation(id);
        if (flag) {
            return new Result(Code.DELETE_OK, "删除成功", flag);
        } else {
            return new Result(Code.DELETE_FAIL, "删除失败", flag);
        }
    }

    //新值怪谈信息
    @PostMapping("/addInformation")
    public Result addInformation(@RequestBody Information information) {
        boolean flag = informationService.addInformation(information);
        if (flag) {
            return new Result(Code.ADD_OK, "添加成功", flag);
        } else {
            return new Result(Code.ADD_FAIL, "添加失败", null);
        }
    }

    //修改怪谈信息
    @PostMapping("/updateInformation/{id}")
    public Result updateInformation(@PathVariable Integer id, @RequestBody Information information) {
        boolean flag = informationService.updateInformation(id, information);
        if (flag) {
            return new Result(Code.ADD_OK, "修改成功", flag);
        } else {
            return new Result(Code.ADD_FAIL, "修改失败", null);
        }
    }






}

